<template>
  <div class="box">
    <!-- <div class="header-name">
      <div>
        <el-avatar
          src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
        ></el-avatar>
        <p class="name">经纪人渠道录件系统</p>
      </div>
    </div> -->
    <div class="tag-group">
      <el-tag
        v-for="(item, index) in data"
        :key="index"
        effect="plain"
        closable
        class="scrollMenu active"
        @close="updateTag(item, index)"
        @click.native="clickTag(item)"
      >
        {{ item.label }}
      </el-tag>
    </div>
    <div class="header-right">
      <img class="headImg" src="../assets/img/userimg.png" />
      <p class="userName">{{ userName }}</p>
      <p @click="signOut" class="el-icon-alarm-clock p1">
        退出
      </p>
    </div>
  </div>
</template>
<script>
import { apiLogout, menuList } from "../assets/js/api";
import $ from "jquery";
export default {
  name: "Navigation",
  props: ["printList"],
  data() {
    return {
      userName: null,
      noauthorize: false,
      authorize: true,
      dataList: [],
      data: [],
      list: []
    };
  },
  created() {
    this.userName = localStorage.getItem("approvedname");
    this.data = this.printList;
    // let url = sessionStorage.getItem("url");
    this.menuList();
  },
  updated() {
    this.scrollMenu();
  },

  methods: {
    getList(treeData, list) {
      treeData.map(item => {
        let { id, url, label, otherId, pid } = item;
        list.push({ id, url, label, otherId, pid });
        if (item.children != null) {
          this.getList(item.children, list);
        }
      });
    },
    // 菜单
    menuList() {
      menuList().then(res => {
        this.list = res.data;
        let listArr = [];
        this.getList(this.list, listArr);
        let url = sessionStorage.getItem("url");
        let printName = listArr.find(item => {
          return item.url == url;
        });
        if (printName != undefined) {
          this.data.push(printName);
        }
        this.$emit("menuShow", this.data);
      });
    },
    signOut() {
      apiLogout().then(() => {
        this.$router.push({ path: "/" });
        localStorage.clear();
        sessionStorage.clear();
      });
    },
    updateTag(item, index) {
      //获取路由路径
      let path = this.$route.path;
      let indexMenu = this.data.findIndex(i => i.label === item.label);
      let indexCurr = this.printList.findIndex(i => i.url === path);
      let urlIndex = indexMenu - 1;
      let urlIndexF = indexMenu;
      if (path == item.url && this.printList.length != 1) {
        //当删除的是第一个
        if (indexMenu == 0) {
          let data = this.printList.find(item => {
            return item.url == this.data[urlIndexF + 1].url;
          });
          if (data.otherId != null && data.otherId != undefined) {
            sessionStorage.setItem("productId", data.otherId);
          }
          this.$router.push(this.data[urlIndexF + 1].url);
          $(".scrollMenu ")
            .eq(urlIndexF)
            .addClass("active")
            .siblings()
            .removeClass("active");
        } else {
          //删除的是后面的
          let data = this.printList.find(item => {
            return item.url == this.data[urlIndex].url;
          });
          if (data.otherId != null && data.otherId != undefined) {
            sessionStorage.setItem("productId", data.otherId);
          }
          this.$router.push(this.data[urlIndex].url);
          sessionStorage.setItem("url", this.data[urlIndex].url);
          $(".scrollMenu ")
            .eq(urlIndex)
            .addClass("active")
            .siblings()
            .removeClass("active");
        }
      } else if (indexCurr > index) {
        $(".scrollMenu ")
          .eq(indexCurr - 1)
          .addClass("active")
          .siblings()
          .removeClass("active");
      }
      this.data = this.data.filter(i => {
        return i != item;
      });
      this.$emit("print", this.data);
    },
    clickTag(item) {
      let path = this.$route.path;
      if (item.url != path) {
        if (item.otherId != null && item.otherId != undefined) {
          sessionStorage.setItem("productId", item.otherId);
        }
        this.$router.push(item.url);
        sessionStorage.setItem("url", item.url);
      }
    },
    //点击图标
    scrollMenu() {
      $(".scrollMenu ").on("click", function() {
        $(this)
          .addClass("active")
          .siblings()
          .removeClass("active");
        let left = $(this).position().left;
        let objWidth = $(this).width();
        let width = $(".tag-group").width();
        let objLeft = objWidth + width;
        if (left > width) {
          $(".tag-group").animate({ scrollLeft: objLeft + "px" }, 500);
        } else if (left < 0) {
          $(".tag-group").animate({ scrollLeft: left + 20 + "px" }, 500);
        }
      });
    }
  }
};
</script>
<style scoped type="less">
.box {
  padding: 0;
  margin: 0;
  width: calc(100% - 210px);
  height: 58px;
  position: fixed;
  right: 0;
  z-index: 99;
  background-color: white;
  overflow: hidden;
  box-shadow: 3px 1px 5px rgb(193, 193, 193);
}

.header-name {
  width: 200px;
  float: left;
  height: 100%;
}
.name {
  float: right;
  line-height: 60px;
  font-size: 14px;
  height: 100%;
  color: #333333;
}
.header-crumb {
  float: left;
  height: 100%;
  margin-left: 20px;
}
.el-breadcrumb {
  line-height: 60px;
}

.header-right {
  float: right;
  font-size: 14px;
  color: #666666;
  height: 100%;
}
.headImg {
  width: 50px;
  height: 50px;
  float: left;
  margin-top: 3px;
  margin-right: 5px;
  border-radius: 25px;
}
/* .el-avatar {
  float: left;
  margin-top: 9px;
} */
.p1 {
  float: left;
  height: 100%;
  line-height: 58px;
  margin-right: 20px;
}
.header-right p:hover {
  cursor: pointer;
}
.tag-group {
  text-align: left;
  float: left;
  height: 58px;
  line-height: 58px;
  width: calc(100% - 450px);
  margin-left: 20px;
  overflow-x: auto; /* 添加横向滚动条 */
  white-space: nowrap; /*不换行*/
  overflow-y: hidden;
  /* overflow: hidden; */
}
::-webkit-scrollbar {
  height: 8px;
  background-color: rgb(241, 243, 244);
  border-radius: 25px;
}
.tag-group .el-tag {
  margin-left: 10px;
  cursor: pointer;
}
/* .el-tag.el-tag--success {
  height: 32px;
  border: 1px solid rgba(238, 238, 238, 1);
  border-radius: 4px;
  color: #333333;
  background: rgba(255, 255, 255, 1);
}
::v-deep .el-tag.el-tag--success .el-tag__close {
  color: #333333;
}
::v-deep .el-tag.el-tag--success:hover .el-tag__close {
  color: rgba(59, 157, 248, 1);
  background: none;
}
::v-deep .el-tag.el-tag--success:hover {
  cursor: pointer;
  border: 1px solid rgba(59, 157, 248, 1);
  border-radius: 4px;
  color: rgba(59, 157, 248, 1);
  background-color: rgba(59, 157, 248, 0.1);
} */
.scrollMenu {
  color: #333333;
  border: 1px #eeeeee solid;
}
::v-deep .el-tag--plain .el-tag__close {
  color: #333333;
}
::v-deep .el-tag--plain .el-tag__close:hover {
  background: none;
}
.scrollMenu.active {
  background-color: rgba(59, 157, 248, 0.2);
  color: #3b9df8;
  border-color: #3b9df8;
}
::v-deep .active .el-icon-close {
  color: #3b9df8;
}
.userName {
  padding-right: 20px;
  border-right: 2px #eeeeee solid;
  height: 20px;
  line-height: 20px;
  margin-top: 20px;
  margin-right: 10px;
  float: left;
}
::v-deep .el-icon-edit-outline:before {
  margin-right: 2px;
}
::v-deep .el-icon-alarm-clock:before {
  margin-right: 2px;
}
</style>
